package com.dongbo.recorder.screenrecord.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.gyf.immersionbar.ImmersionBar;
import com.dongbo.recorder.screenrecord.R;
import com.dongbo.recorder.screenrecord.adapter.EditAdapter;
import com.dongbo.recorder.screenrecord.base.BaseActivity;
import com.dongbo.recorder.screenrecord.bean.EditVideoBean;
import com.dongbo.recorder.screenrecord.utils.FileUtils;
import com.dongbo.recorder.screenrecord.widget.ResizeAbleSurfaceView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @description: 视频编辑界面
 */
public class EditActivity extends BaseActivity {
    @BindView(R.id.cut_back)
    ImageView cutBack;
    @BindView(R.id.paly_logo)
    ImageView palyLogo;
    @BindView(R.id.edit_sv)
    ResizeAbleSurfaceView editSv;
    @BindView(R.id.edit_start)
    ImageView editStart;
    @BindView(R.id.edit_rv)
    RecyclerView editRv;

    @BindView(R.id.caijian)
    View caijian;
    @BindView(R.id.biansu)
    View biansu;
    @BindView(R.id.daofang)
    View daofang;

    private MediaPlayer mediaPlayer;
    private String videoPath;

    private EditAdapter editAdapter;
    private List<EditVideoBean> editList = new ArrayList<>();

    @Override
    protected void onRestart() {
        super.onRestart();
        if (!TextUtils.isEmpty(videoPath)) {
            palyLogo.setVisibility(View.VISIBLE);
            Glide.with(this).load(videoPath).into(palyLogo);
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_edit;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void initView() {
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarColor(R.color.white).statusBarDarkFont(true).init();
        initAdapter();
    }

    @Override
    protected void initData() {
        //每次进来清除一次缓存文件
        FileUtils.deleteDirectory(FileUtils.getCacheFile());
        videoPath = getIntent().getStringExtra("videoPath");
        Glide.with(this).load(videoPath).into(palyLogo);
        editList.add(new EditVideoBean("裁剪", ""));
        editList.add(new EditVideoBean("变速", ""));
        editList.add(new EditVideoBean("倒放", ""));
        editAdapter.setList(editList);
        editAdapter.setChoose(-1);
        Glide.with(this).load(videoPath).into(palyLogo);
    }

    @Override
    protected void initListener() {
        editSv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    play();
                }
                return true;
            }
        });
    }

    @OnClick({R.id.cut_back, R.id.edit_start, R.id.caijian, R.id.biansu, R.id.daofang})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cut_back:
                finish();
                break;
            case R.id.edit_start:
                break;
            case R.id.caijian:
                Intent intent = new Intent(EditActivity.this, CutActivity.class);
                intent.putExtra("videoPath", videoPath);
                startActivity(intent);
                break;
            case R.id.biansu:
                Intent intent1 = new Intent(EditActivity.this, SpeedActivity.class);
                intent1.putExtra("videoPath", videoPath);
                startActivity(intent1);
                break;
            case R.id.daofang:
                Intent intent2 = new Intent(EditActivity.this, SequenceActivity.class);
                intent2.putExtra("videoPath", videoPath);
                startActivity(intent2);
                break;
        }
    }

    private void initAdapter() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        editRv.setLayoutManager(linearLayoutManager);
        if (editAdapter == null) {
            editAdapter = new EditAdapter();
            editAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    editAdapter.setChoose(position);
                    Intent intent = null;
                    switch (position) {
                        //裁剪
                        case 0:
                            intent = new Intent(EditActivity.this, CutActivity.class);
                            break;
                        //变速
                        case 1:
                            intent = new Intent(EditActivity.this, SpeedActivity.class);
                            break;
                        //倒放
                        case 2:
                            intent = new Intent(EditActivity.this, SequenceActivity.class);
                            break;
                    }
                    intent.putExtra("videoPath", videoPath);
                    startActivity(intent);
                }
            });
        }
        editRv.setAdapter(editAdapter);
    }

    public void play() {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            //设置类型
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//            int width = (int) FFmpegCommand.getInfoSync(videoPath, Attribute.WIDTH);
//            int height = (int) FFmpegCommand.getInfoSync(videoPath, Attribute.HEIGHT);
//            editSv.resize(width, height);
            editSv.resize(1080, 1920);
            try {
                /* 为MediaPlayer 设置数据源 */
                mediaPlayer.setDataSource(videoPath);
                /* 准备 */
                mediaPlayer.prepare();
                //将播放器捕捉的画面展示到SurfaceView画面上
                mediaPlayer.setDisplay(editSv.getHolder());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            palyLogo.setVisibility(View.GONE);
            mediaPlayer.start();
            editStart.setVisibility(View.GONE);
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    editStart.setVisibility(View.VISIBLE);
                    palyLogo.setVisibility(View.VISIBLE);
                    Glide.with(EditActivity.this).load(videoPath).into(palyLogo);
                }
            });
        } else if (mediaPlayer.isPlaying()) {
            editStart.setVisibility(View.VISIBLE);
            mediaPlayer.pause();
        } else {
            editStart.setVisibility(View.GONE);
            palyLogo.setVisibility(View.GONE);
            mediaPlayer.setDisplay(editSv.getHolder());
            mediaPlayer.start();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                editStart.setVisibility(View.VISIBLE);
                mediaPlayer.pause();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.release();
        }
    }
}
